//
//  RegeditViewController.m
//  OneWordNew
//
//  Created by qianfeng on 15-7-6.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "RegeditViewController.h"
#import "LZXHttpRequest.h"
#import "LoginViewController.h"
@interface RegeditViewController ()
{
    LZXHttpRequest *_httpRequest;
}
@end

@implementation RegeditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _httpRequest=[[LZXHttpRequest alloc] init];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.nameText resignFirstResponder];
    [self.emailText resignFirstResponder];
    [self.mimaText resignFirstResponder];
}
- (void)showAlerViewWithtitle:(NSString *)title message:(NSString *)message{
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    [alertView show];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)finishClick:(UIButton *)sender {
    if (self.nameText.text==nil||self.mimaText.text==nil) {
        [self showAlerViewWithtitle:@"警告" message:@"用户名或者密码不能为空"];
        return;
    }
    NSString *username=[self.emailText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *nickname=[self.nameText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *password=[self.mimaText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSDictionary *dict=@{@"username":username,
                         @"password1":password,
                         @"key":@"3a4b2a12539a916c040d069ae8ac8310",
                         @"password2":password,
                         @"nickname":nickname,};
    NSString *url=[kOneUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
       [_httpRequest postRequestWithUrl:url param:dict contentType:@"application/x-www-form-urlencoded" success:^(LZXHttpRequest *httpRequest) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:httpRequest.downloadData options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"]  isEqual:@(0)]) {
            [self showAlerViewWithtitle:@"恭喜" message:@"注册成功"];
            LoginViewController *page=[[LoginViewController alloc] init];
            
            [self.navigationController pushViewController:page animated:YES];
            
            
        }else{
            [self showAlerViewWithtitle:@"警告" message:dict[@"msg"]];
        }
        
        
    } failed:^(NSError *error) {
        
    }];
    
    
}
@end
